package com.zx.baofu.alcohol.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.regexp.internal.RE;
import com.zx.baofu.alcohol.model.*;
import com.zx.baofu.alcohol.service.*;
import com.zx.baofu.alcohol.util.MyStringUtil;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import sun.rmi.runtime.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/user", method = RequestMethod.POST)
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private SettingService settingService;
    @Autowired
    private PrizeService prizeService;
    @Autowired
    private PeriodsService periodsService;
    @Autowired
    private PeriodsAddressService periodsAddressService;

    /**
     *
     * @param unionid
     * @param nickName
     * @param img
     * @param longitude gps坐标
     * @param latitude gps坐标
     * @return {"code":200,"msg":"添加用户成功"}
     *  {"code":500,"msg":"用户已存在"}
     */
    @RequestMapping(value = "/addUser")
    public Result addUser(String unionid, String nickName, String img, Double longitude, Double latitude){
        return userService.addUser(unionid, nickName, img, longitude, latitude);
    }

    /**
     *
     * @param helpUnionId
     * @param helpedUnionId
     * @return {"code":200,"msg":"帮助好友倒酒成功"}
     *  {"code":500,"msg":"不能给自己倒酒"}
     */
    @RequestMapping(value = "/helpUser")
    public Result helpUser(String helpUnionId, String helpedUnionId){
        return userService.helpUser(helpUnionId, helpedUnionId);
    }

    /**
     *
     * @param unionid
     * @return {"code":200,"msg":"恭喜您中奖","data":{"id":1,"orderNum":"42112","userUnionid":"1","prizeName":"品鉴酒38度","prizeValue":188.0,"prizeNum":1}}
     *  {"code":500,"msg":"您积累的酒量还不够500ml，请继续邀请好友倒酒"}
     */
    @RequestMapping(value = "/userLottery")
    public Result userLottery(String unionid){
        System.out.println("当前id"+unionid);
        return userService.userLottery(unionid);
    }

    /**
     * {"code":200,"msg":"保存信息成功"}
     * @param unionid
     * @param orderNum
     * @param name
     * @param mobile
     * @param address
     * @return
     */
    @RequestMapping(value = "/userWriteAddress")
    public Result userWriteAddress(String unionid, String orderNum, String name, String mobile, String address, String area, String town){
        if (MyStringUtil.isNullorEmpty(mobile)){
            return new Result().fail("手机号不能为空");
        }
        if (MyStringUtil.isNullorEmpty(address)){
            return new Result().fail("地址不能为空");
        }
        return orderService.userWriteAddress(unionid, orderNum, name, mobile, address, area, town);
    }

    /**
     *
     * @param unionid
     * @return {"code":200,"msg":"查询成功","data":{"id":1,"orderNum":"42112","userUnionid":"1","created":"2019-11-29","prizeName":"品鉴酒38度","prizeValue":188.0,"prizeNum":1}}
     *  {"code":200,"msg":"查询成功","data":{"id":1,"orderNum":"42112","userUnionid":"1","created":"2019-11-29","prizeName":"品鉴酒38度","prizeValue":188.0,"prizeNum":1,"contactName":"zjl","contactMobile":"18132773004","contactAddress":"地址信息jhsad"}}
     */
    @RequestMapping(value = "/userOrder", method = RequestMethod.GET)
    public Result userOrder(String unionid){
        return orderService.userOrder(unionid);
    }

    /**
     *
     * @return {"code":200,"msg":"查询完成","data":{"id":1,"title":"这是标题","img":"https://img.hefenmall.com/u=1601159258,3016026220&fm=26&gp=0.jpg","content":"这是描述"}}
     */
    @RequestMapping(value = "/shareMsg", method = RequestMethod.GET)
    public Result getShareMsg(){
        ShareMsg sm = settingService.getShareMsg();
        return new Result().success("查询完成", sm);
    }

    /**
     *
     * @return {"code":200,"msg":"查询完成","data":{"id":1,"prizeName":"38度酒","prizeValue":18.8,"prizeNum":10,"start":"2019-12-03 12:00:00","end":"2019-12-08 12:30:00","manufacturer":"第三医用酒精厂","rule":"集赞品好酒","description":"点赞就完事了"}}
     */
    @RequestMapping(value = "/getActiveDetail", method = RequestMethod.GET)
    public Result getActiveDetail(){
        ActiveTime at = settingService.getActiveTime();
        return new Result().success("查询完成", at);
    }

    /**
     *
     * @param unionid
     * @return {"code":200,"msg":"查询完成","data":{"id":1,"unionid":"1","nickname":"藏家林","img":"http://p1.pstatp.com/large/pgc-image/49d7b3459a4b47babe886c0a5b02dcae","lotteryed":1,"alcoholNum":500,"win":1}}
     */
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
    public Result getUserInfo(String unionid){
        Result result = userService.getUserInfo(unionid);
        return result;
    }

    /**
     *
     * @return {"code":200,"msg":"查询成功","data":[{"prizeName":"品鉴酒38度","prizeNum":1,"prizeValue":0.0,"nickname":"藏家林","img":"http://p1.pstatp.com/large/pgc-image/49d7b3459a4b47babe886c0a5b02dcae"}]}
     */
    @RequestMapping(value = "/getWinList", method = RequestMethod.GET)
    public Result getWinList(){
        return userService.getWinList();
    }

    @RequestMapping(value = "/getShopList", method = RequestMethod.GET)
    public Result getShopList(){
        return userService.getShopList();
    }

    @RequestMapping(value = "/getHelpRecord", method = RequestMethod.GET)
    public Result getHelpRecord(String unionid){
        return userService.getHelpRecord(unionid);
    }

    @RequestMapping(value = "/cityList", method = RequestMethod.GET)
    public Result cityList(){
        return userService.cityList();
    }

    @RequestMapping(value = "getPrizeById",method = RequestMethod.GET)
    public Result getPrizeById(Long id){return prizeService.getPrizeById(id);}
    @RequestMapping(value = "updatePrize",method = RequestMethod.POST)
    public Result updatePrize(Prize prize){return prizeService.updatePrize(prize);}
    @RequestMapping(value = "insertPrize",method = RequestMethod.POST)
    public Result insertPrize(Prize prize){return prizeService.insertPrize(prize);}

    /**修改中奖概率注：并判断是否等于100*/
    @RequestMapping(value = "updateAllProbability",method = RequestMethod.POST)
    public Result updateAllProbability( Integer [] pIds, Integer []  probabilitys){
        List<LotteryProbability> list=new ArrayList<>();
        /*把前端传来的数组转成概率对象在进行批量修改*/
        for (int j = 0; j < pIds.length; j++) {
            LotteryProbability lotteryProbability=new LotteryProbability();
            lotteryProbability.setId(pIds[j]);
            lotteryProbability.setProbability(probabilitys[j]);
            list.add(lotteryProbability);
        }
        Result result=orderService.updateProbability(list);
        return result;
    }
    @RequestMapping(value = "getUserOrder",method = RequestMethod.GET)
    public Result getUserOrder(Integer id){
        Result result=orderService.getUserOrder(id.longValue());
        return result;
    }
    @RequestMapping(value = "updatePeriods",method = RequestMethod.POST)
    public Result updatePeriods(Long id,String startDate,String endDate,String periodsName){
        Result result=periodsService.updatePeriods(id,startDate,endDate,periodsName);
        return result;
    }
    @RequestMapping(value = "updatePeriodsAddress",method = RequestMethod.POST)
    public Result updatePeriodsAddress(PeriodsAddress periodsAddress){
        Result result=periodsAddressService.updatePeriodsAddress(periodsAddress);
        return result;
    }
    @RequestMapping(value = "getByPeriodsId",method = RequestMethod.GET)
    public Result getByPeriodsId(Integer periodsId){
        Result result=periodsAddressService.getByPeriodsId(periodsId.longValue());
        return result;
    }
    //返回给前端
}
